package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.BrandDao;
import com.sailfish.springbootdemo.dao.db1.BrandTypeDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.BrandType;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BrandTypeServiceImpl implements BrandTypeService {

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private BrandTypeDao brandTypeDao;

    @Autowired
    private CommonJudgeCanDeleteService commonService;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "品牌/型号管理";

    @Override
    public List<BrandType> getAllBrandType(String searchValue, Integer brandId) {
        List<BrandType> list = brandTypeDao.getAllBrandType(searchValue, brandId);
        for (BrandType brandType : list) {
            brandType.setBrand(brandDao.getBrandById(brandType.getBrandId()));
        }
        return list;
    }

    @Override
    public BrandType getBrandTypeById(Integer brandTypeId) {
        return brandTypeDao.getBrandTypeById(brandTypeId);
    }

    @Override
    public Result deleteBrandTypeById(Integer brandTypeId) {
        try {
            if (brandTypeId == null || "".equals(brandTypeId.toString())) {
                return ResultUtil.error(500, "缺少参数brandTypeId", "Miss Param brandTypeId", null);
            }

            String userId = UserHeaderHolder.getUserId();
            BrandType oldBrandType = brandTypeDao.getBrandTypeById(brandTypeId);

            Map<String, Integer> stringIntegerMap = commonService.judgeCanDeleteBrandType(brandTypeId);
            if (stringIntegerMap.get("pcbaDeviceResourceNum") > 0) {
                return ResultUtil.error(500,
                        "禁止删除，当前项" + stringIntegerMap.get("pcbaDeviceResourceNum") + "处关联设备资源池",
                        "Forbidden Delete", null);
            }
            Integer integer = brandTypeDao.deleteBrandTypeById(brandTypeId);
            if (integer > 0) {
                // 操作记录
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, new Date(), JSONObject.toJSONString(oldBrandType), null));
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result addOrUpdateBrandType(String mode, BrandType brandType) {
        try {
            if (brandType == null) {
                return ResultUtil.error(500, "缺少参数brandType", "Miss Param brandType", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("edit".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }

            String userId = UserHeaderHolder.getUserId();

            if ("edit".equals(mode)) {
                if (brandType.getBrandTypeId() == null || "".equals(brandType.getBrandTypeId().toString())) {
                    return ResultUtil.error(500, "缺少参数brandId", "Miss Param brandId", null);
                }

                List<BrandType> brandTypeList = brandTypeDao.judgeUniqueBrandType(brandType.getBrandTypeId(), brandType.getBrandId(), brandType.getBrandTypeName());
                if (brandTypeList.size() > 0) {
                    return ResultUtil.error(500, "修改失败，存在相同名字", "Operate Fail", null);
                }

                BrandType oldBrandType = brandTypeDao.getBrandTypeById(brandType.getBrandTypeId());

                brandType.setUpdateTime(new Date());
                Integer integer = brandTypeDao.updateBrandType(brandType);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldBrandType), JSONObject.toJSONString(brandType)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {
                List<BrandType> brandTypeByName = brandTypeDao.getBrandTypeByName(brandType.getBrandTypeName(), brandType.getBrandId());

                if (brandTypeByName.size() > 0) {
                    return ResultUtil.error(500, "该品牌下已存在此型号", "Already Exist", null);
                }

                brandType.setCreateTime(new Date());
                Integer integer = brandTypeDao.addBrandType(brandType);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, new Date(), null, JSONObject.toJSONString(brandType)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
